package constant.milk.toilet;

import constant.milk.toilet.broadcast.PushBroadcast;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class FirstActivity extends Activity
{
	private TextView titleTextView;
	private TextView tipTextView;
	
	private View startView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.frist_activity);
		
		setFindViewId();
		setOnClick();
		setData();
		
//		ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
	}

	protected void setFindViewId()
	{
		titleTextView = (TextView) findViewById(R.id.firstTitleTextView);
		tipTextView = (TextView) findViewById(R.id.firstTipTextView);
		
		startView = findViewById(R.id.firstStartView);
	}

	protected void setOnClick()
	{
		startView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
				SharedPreferences.Editor e = p.edit();
				
				e.putInt(getString(R.string.prefFirstStart), 1);
				e.putInt(getString(R.string.prefPushAlarm), 1);
				e.commit();
				
				Intent intent = new Intent(FirstActivity.this, PushBroadcast.class);    
				PendingIntent sender = PendingIntent.getBroadcast(FirstActivity.this, 1, intent, 
						PendingIntent.FLAG_CANCEL_CURRENT);
				
		        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		        manager.cancel(sender);
		        
		        int version = Build.VERSION.SDK_INT;
				
				if (version >= 19)
				{
					manager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (24 * 60 * 60000), sender);
				}
				else
				{
					manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (24 * 60 * 60000), sender);
				}
				
				Intent intent1 = new Intent(FirstActivity.this, MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startActivity(intent1);
				finish();
			}
		});
	}

	protected void setData()
	{
		String title = "";
		
		title = getString(R.string.app_name) + "이 올바른 배변 습관을 도와 드려요!";
		
		titleTextView.setText(title);
		
		String tip = "";
		
		tip = "화장실을 만족스럽게 갔다 온 것 (쾌변) "
				+ "만으로도 몸이 날아갈 듯 상쾌한 기분을 느낀 적 없으셨나요? "
				+ "자신의 그 날 그 날의 배변 상태를 체크하여 전국민이 쾌변 하는 그 날을 위해 '";
		
		tip += getString(R.string.app_name) + "' 어플을 만들었습니다.";
		
		tipTextView.setText(tip);
	}
}
